import json
import re
import requests
import datetime
import xlwt

today = datetime.date.today().strftime('%Y%m%d')

def crawl_dxy_data():
    """
    爬取丁香园实时统计数据
    """
    response = requests.get('https://ncov.dxy.cn/ncovh5/view/pneumonia')  # 发送get请求

    url_text = response.content.decode()  # 获取响应的html页面
    url_content = re.search(r'window.getAreaStat = (.*?)}]}catch',   # re.search()用于扫描字符串以查找正则表达式模式产生匹配项的第一个位置，然后返回相应的match对象
                            url_text, re.S)                         # 在字符串a中，包含换行符\n，这种情况下：如果不适用re.S参数，则只在每一行内进行匹配，如果一行没有，就换下一行重新开始匹配
    texts = url_content.group()  # 获取匹配正则表达式的整体结果
    content = texts.replace('window.getAreaStat = ', '').replace('}catch', '')  # 去除多余字符
    work_book = xlwt.Workbook(encoding="utf-8")
    work_sheet = work_book.add_sheet('疫情', cell_overwrite_ok=True)
    col = ('省份', '简称', '现存确诊', '累计确诊', '死亡', '治愈')
    for i in range(0, 6):
        work_sheet.write(0, i, col[i])
    json_data = json.loads(content)
    for index, value in enumerate(json_data):
        work_sheet.write(index+1, 0, value['provinceName'])
        work_sheet.write(index+1, 1, value['provinceShortName'])
        work_sheet.write(index+1, 2, value['currentConfirmedCount'])
        work_sheet.write(index+1, 3, value['confirmedCount'])
        work_sheet.write(index+1, 4, value['suspectedCount'])
        work_sheet.write(index+1, 5, value['curedCount'])
    with open('data/' + today + '.json', 'w', encoding='UTF-8') as f:
        json.dump(json_data, f, ensure_ascii=False)
    work_book.save(r'data/' + today + '.xls')

crawl_dxy_data()